<template>
  <div class="edit-dialog">
    <el-dialog
      title="修改密码"
      :visible.sync="dialog"
      :close-on-click-modal="false"
      :show-close="true"
      width="400px"
      center
      @close="handleClose"
    >
      <div class="content userinfo-content">
        <el-form
          :model="addFormData"
          label-position="right"
          ref="ruleFormRef"
          :rules="rules"
          label-width="auto"
          class="form"
        >
          <el-form-item prop="oldPasswd" label="原密码">
            <el-input type="password" placeholder="请输入原密码" v-model="addFormData.oldPasswd" show-password></el-input>
          </el-form-item>
          <el-form-item prop="newPasswd" label="新密码">
            <el-input type="password" placeholder="请输入新密码" v-model="addFormData.newPasswd" show-password></el-input>
          </el-form-item>
          <el-form-item prop="confirmPasswd" label="确认密码">
            <el-input type="password" placeholder="请再次输入新密码" v-model="addFormData.confirmPasswd" show-password></el-input>
          </el-form-item>
        </el-form>
        <el-row style="text-align: center">
          <el-button type="primary" :loading="submitLoading" icon="el-icon-check" @click="submitForm('ruleFormRef')">提 交</el-button>
          <el-button icon="el-icon-close" @click="handleClose">取 消</el-button>
        </el-row>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { Message } from "element-ui";
import { resetPasswd } from "@/api/user";
export default {
  name: "resetPwd",
  components: {},
  props: {
    showDialog: false
  },
  data() {
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.addFormData.newPasswd) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      submitLoading: false,
      addFormData: {},
      rules: {
        oldPasswd: [
          { required: true, message: "请输入原密码", trigger: "blur" }
        ],
        newPasswd: [
          { required: true, message: "请输入新密码", trigger: "blur" }
        ],
        confirmPasswd: [{ validator: validatePass2, trigger: "blur" }]
      }
    };
  },
  computed: {
    dialog: function() {
      return this.showDialog;
    }
  },
  watch: {
    dialog(newVal, oldVal) {
      if (newVal) {
        //this.addFormData = this.formData;
        //this.getMessage()
      }
    }
  },
  methods: {
    // 取消事件
    handleClose() {
      this.$emit("editPasswordDialogFun", false);
    },
    submitForm(formName) {
      this.addFormData.userName = this.$store.state.user.userInfo.name;
      this.submitLoading = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.submitLoading = false;
          resetPasswd(this.addFormData).then(res => {
            if (res.code == "200") {
              Message.success("修改成功！");
              this.$emit("editPasswordDialogFun", false);
            }
          });
        } else {
          this.submitLoading = false;
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.edit-pwd {
  padding-top: 80px;

  .form {
    width: 500px;
    margin: auto;

    .title {
      display: block;
      font-size: 16px;
      // font-weight: bold;
      line-height: 30px;
      color: #212121;
    }

    .phone-num {
      width: 100%;
      text-align: left;
    }

    .code {
      width: 100%;
      display: flex;
    }

    .btn {
      margin: 0 20px;
    }
  }
}
</style>
